package com.claire.controller;

import com.claire.bean.Apply;
import com.claire.bean.Student;
import com.claire.common.ApiResponse;
import com.claire.common.BaseController;
import com.claire.service.ApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/apply")
public class ApplyController extends BaseController {

    @Autowired
    private ApplyService applyService;

    /**
     * 提交申请
     * @param apply
     * @param request
     * @return
     */
    @RequestMapping(value="/createApply",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public ApiResponse createApply(@RequestBody Apply apply, HttpServletRequest request){
        Student student=getLoginStudent(request);//获取当前申请用户
        apply.setApplyFromId(student.getId());
        apply.setApplyUsername(student.getUsername());
        return success(applyService.createNewApply(apply));
    }

    /**
     * 管理员获取所在社团收到的  申请信息
     * 普通用户通过自己的id获取自己的申请信息
     * @param current
     * @param size
     * @param request
     * @return
     */
    @RequestMapping(value = "/getApply",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public ApiResponse getApply(@RequestParam Long current,@RequestParam Long size,HttpServletRequest request){
        Student student=getLoginStudent(request);
        if(student.getIsManager()>0){//是管理员
            return success(applyService.getApply(current,size,student.getId()));
        }else {//不是管理员
            return success(applyService.getApplyById(current,size,student.getId()));
        }

    }

    /**
     * 通过申请
     * @param apply
     * @return
     */
    @RequestMapping(value = "/passApply",method = RequestMethod.POST)
    public ApiResponse passApply(@RequestBody Apply apply){
        return success(applyService.passApply(apply));
    }

    /**
     * 拒绝申请
     * @param apply
     * @return
     */
    @RequestMapping(value = "/rejectApply",method = RequestMethod.POST)
    public ApiResponse rejectApply(@RequestBody Apply apply){
        return success(applyService.rejectApply(apply));
    }

    /**
     * 删除申请
     * @param apply
     * @return
     */
    @RequestMapping(value = "/deleteApply",method = RequestMethod.POST)
    public ApiResponse deleteApply(@RequestBody Apply apply){
        return success(applyService.deleteApply(apply));
    }

}
